package cn.iocoder.yudao.module.lowcode.dal.mysql.datasource;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.lowcode.controller.admin.datasource.vo.DatasourceConfigPageReqVO;
import cn.iocoder.yudao.module.lowcode.dal.dataobject.datasource.DatasourceConfigDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 数据源配置 Mapper
 *
 * @author YuDao
 */
@Mapper
public interface DatasourceConfigMapper extends BaseMapperX<DatasourceConfigDO> {

    default PageResult<DatasourceConfigDO> selectPage(DatasourceConfigPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DatasourceConfigDO>()
                .likeIfPresent(DatasourceConfigDO::getName, reqVO.getName())
                .eqIfPresent(DatasourceConfigDO::getType, reqVO.getType())
                .eqIfPresent(DatasourceConfigDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(DatasourceConfigDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DatasourceConfigDO::getId));
    }

    default DatasourceConfigDO selectByName(String name) {
        return selectOne(DatasourceConfigDO::getName, name);
    }

    default List<DatasourceConfigDO> selectActiveDataSources() {
        return selectList(new LambdaQueryWrapperX<DatasourceConfigDO>()
                .eq(DatasourceConfigDO::getStatus, 1) // 1表示启用状态
                .orderByDesc(DatasourceConfigDO::getId));
    }
}